var message = function (title, content, type, time) {
    if (time == null || time == 0) {
        time = 1000;
    }
    var parentdiv = $('<div class ="tips" ></div>');
    var box = $(".tipsBox");
    var typeDiv = $('<i class="iconfont" ></i>');
    if (type == "error") {
        typeDiv.addClass("error");
        typeDiv.addClass("icon-error");
    } else if (type == "success") {
        typeDiv.addClass("success");
        typeDiv.addClass("icon-success");
    }
    var titleSpan = $('<span></span>');
    titleSpan.text(title);
    var contentSpan = $('<span class="tipsContent"></span>');
    contentSpan.text(content);

    var closeP = $('<p class="iconfont close" onclick="message_close($(this))">&#xe605;</p>');
    typeDiv.appendTo(parentdiv);
    titleSpan.appendTo(parentdiv);
    contentSpan.appendTo(parentdiv);
    closeP.appendTo(parentdiv);
    parentdiv.appendTo(box);
    parentdiv.animate({left: 0}, 400);
    setTimeout(function () {
        parentdiv.fadeOut(500, function () {
            parentdiv.remove();
        });
    }, (time + (box.children(".tips").length * 500)));
};


var message_close = function (p) {
    p.parent().fadeOut(500, function () {
        p.parent().remove();
    });
};

window.onload = function () {
    var window_width = $(window).width();
    var login_width = $("#login").width();
    var margin_left = (window_width - login_width) / 2;
    $("#login").css({"margin-left": margin_left});

}


var crossMessaeg = function (message, type) {
    localStorage['message'] = message;
    localStorage['type'] = type;
};

var initialize_after = function () {
    var type = localStorage['type'];
    var mes = localStorage['message'];
    if (type != "" && type != undefined && mes != "" && mes != undefined) {
        load(document.body, 1500);
        setTimeout(function () {
            message(mes, "", type, 1000);
            localStorage.removeItem('message');
            localStorage.removeItem("type");
        }, 2000);
    }
};

var initialize_before = function () {
    var type = localStorage['type'];
    var mes = localStorage['message'];
    if (type != "" && type != undefined && mes != "" && mes != undefined) {
        message(mes, "", type, 1000);
        localStorage.removeItem('message');
        localStorage.removeItem("type");

    }
};

var dialog_show = function () {
    $(".dialog").show();
    // setTimeout(function () {
    //     $(".dialog_content").fadeIn();
    // }, 200);

};

var dialog_close = function () {
    // $(".dialog_content").fadeOut();
    setTimeout(function () {
        $(".dialog").fadeOut(50);
    }, 100);
};

var onSwitch = function () {
    return $('  <div class="switch">\n' +
        '                                <input type="checkbox" id="light" style="display: none" checked>\n' +
        '                                <div class="switch-rq ">\n' +
        '                                    <span class="switch-on">On</span>\n' +
        '                                    <div class="switch-none"></div>\n' +
        '                                    <span class="switch-off">off</span>\n' +
        '                                </div>\n' +
        '                            </div>')
};

var offSwitch = function () {
    return $('   <div class="switch">\n' +
        '                                <input type="checkbox" id="fan" style="display: none">\n' +
        '                                <div class="switch-rq off">\n' +
        '                                    <span class="switch-on">On</span>\n' +
        '                                    <div class="switch-none"></div>\n' +
        '                                    <span class="switch-off">off</span>\n' +
        '                                </div>\n' +
        '                            </div>')
};

var errorSwitch = function () {
    return $('<div class="error-switch">上报故障</div>')
};

var postPage = function (url, dataName, data) {
    var form = $('<form method="post"></form>');
    form.attr("action", url);
    var hidInput = $("<input type ='hidden' />");
    hidInput.attr("name", dataName);
    hidInput.attr("value", data);
    hidInput.appendTo(form);
    form.appendTo($(document.body));
    form.submit();
};

var load = function (selector, time) {
    var loadDiv = $('<div class = "load"> <i class="iconfont">&#xe66c;</i></div>');
    loadDiv.appendTo($(selector));
    setTimeout(function () {
        loadDiv.fadeOut();
        setTimeout(function () {
            loadDiv.remove();
        }, 100)

    }, time);
    return loadDiv;
};

var load_close = function (loadDiv) {
    loadDiv.fadeOut();
    setTimeout(function () {
        loadDiv.remove();
    }, 100)
};

$(".SHinput").children("input").blur(function () {

    if ($(this).val() != "" && $(this).parent().children(".errorMsg").text() == "") {
        $(this).parent().removeClass("input_error");
        $(this).parent().addClass("input_success");
        $(this).parent().children(".icon-error").removeClass("error");
        $(this).parent().children(".icon-success").addClass("success");
    } else if ($(this).val() == "" || $(this).parent().children(".errorMsg").text() != "") {
        $(this).parent().removeClass("input_success");
        $(this).parent().addClass("input_error");
        $(this).parent().children(".icon-success").removeClass("success");
        $(this).parent().children(".icon-error").addClass("error");
    }
});
$(".SHinput").children("textarea ").blur(function () {
    if ($(this).val() != "" && $(this).parent().children(".errorMsg").text() == "") {
        $(this).parent().removeClass("input_error");
        $(this).parent().addClass("input_success");
        $(this).parent().children(".icon-error").removeClass("error");
        $(this).parent().children(".icon-success").addClass("success");
    } else if ($(this).val() == "" || $(this).parent().children(".errorMsg").text() != "") {
        $(this).parent().removeClass("input_success");
        $(this).parent().addClass("input_error");
        $(this).parent().children(".icon-success").removeClass("success");
        $(this).parent().children(".icon-error").addClass("error");
    }
});
// var tableSetting = function () {
//     var tD = $(".tableContent").children("tr").first().children("td");
//     $(".tableTitle").children("th").each(function () {
//         var width = tD[0].getBoundingClientRect().width;
//         // console.log($(this)[0]);
//         $(this).css({"width": width - 1});
//         tD = tD.next();
//     });
//     // $(".bottom").css({"min-width":$(".tableContent").width()})
// };

var addTd = function (value) {
    var td = $('<td></td>');
    td.text(value);
    // td.append(tr);
    return td;
};
var addTdHidId = function (value, id) {
    var td = $('<td  style="display: none"></td>');
    var span = $('<span style="display: none"></span>');
    span.text(id);
    td.text(value);
    span.appendTo(td);
    return td;
};
var addRealOption = function (value) {
    var option = $("<option></option>");
    option.text(value);
    return option;
};

var addOption = function (value, id, width) {
    var li = $('<li></li>');
    if (!isNull(width)) {
        li.width(width);
    }
    var div = $('<div></div>');
    var span = $('<span></span>');
    span.text(value);
    div.text(id);
    span.appendTo(li);
    div.appendTo(li);
    return li;
};

var isNull = function (value) {
    return (value == "") || (value == 0) || (value == null) || (value == undefined);
};

var isNulltoWhat = function (field, value) {
    if (isNull(field)) {
        field = value;
    }
};
var errorMessage = function (msg) {
    message(msg, "请重新尝试或者联系管理员", "error", 1500);
};

var isnullTONull = function (field) {
    if (isNull(field)) {
        return ""
    } else return field;
};
var listSetting = function (totalPages, pageNumber) {
    if (pageNumber == 1 && pageNumber == totalPages) {
        // console.log(1);
        $("#next").addClass("notAllow");
        $("#last").addClass("notAllow");
    }
    if (pageNumber == 1 && pageNumber < totalPages) {
        // console.log(2);
        $("#last").addClass("notAllow");
        $("#last").attr("disable", "disable");
        $("#next").removeClass("notAllow");
    }
    if (pageNumber == totalPages && pageNumber != 1) {
        // console.log(3);
        $("#next").addClass("notAllow");
        $("#last").removeClass("notAllow");
    }
    if (pageNumber > 1 && pageNumber < totalPages) {
        $("#last").removeClass("notAllow");
        $("#next").removeClass("notAllow");
    }

};

var getNothingBackground = function () {
    return $('<div style="position: absolute; left: 40%;top: 20%;">' +
        '<img src="/static/img/bg-03.jpg">' +
        '</div>');
};


$(".SHselect-father").click(function (e) {
    // console.log( $(".SHselect-son").children("ul").children("li"));
    $(".SHselect-son").slideUp();
    if (!$(this).parent().hasClass("notAllow")) {
        if ($(this).children(".SHselect-son").is(":hidden")) {
            $(this).children(".SHselect-son").slideDown();
            e.stopPropagation();
        }
        if ($(this).children(".SHselect-son").is(":visible")) {
            $(this).children(".SHselect-son").slideUp;
            e.stopPropagation();
        }

    }
});


var switch_onload = function () {
    $(".switch-rq").click(function () {
            var my = $(this);
            var zhezhao = $('<div class="switch-not-allow" > </div>');
            zhezhao.appendTo($(this).parent());
            $(this).addClass("notClick");
            if ($(this).hasClass("on") || !$(this).hasClass("off")) {

                $(this).stop().animate({"left": "-50px"}, 500);
                $(this).parent().children("input").prop("checked", false);
                setTimeout(function () {
                    my.removeClass("on");
                    my.addClass("off");
                }, 500)
            } else if ($(this).hasClass("off")) {

                $(this).stop().animate({"left": "0"}, 500);
                $(this).parent().children("input").prop("checked", true);
                setTimeout(function () {
                    my.removeClass("off");
                    my.addClass("on");
                }, 500)

            }
            setTimeout(function () {
                zhezhao.remove();
                my.removeClass("notClick");

            }, 3000);

        }
    );

};

var formatTime = function (time) {
    var fmt = "yyyy-MM-dd hh:mm:ss";
    var date = new Date(time);
    var o = {
        "M+": date.getMonth() + 1,                 //月份
        "d+": date.getDate(),                    //日
        "h+": date.getHours(),                   //小时
        "m+": date.getMinutes(),                 //分
        "s+": date.getSeconds(),                 //秒
        "q+": Math.floor((date.getMonth() + 3) / 3), //季度
        "S": date.getMilliseconds()             //毫秒
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

var father_pro = function () {
    return $(" <div class=\"father_pro \"></div>");
};
var in_pro = function (num, msg) {
    var son_pro = $(" <div class=\"son_pro\"> </div>");
    var msg_pro = $("  <div class=\"msg_pro\"></div>");
    var line_pro = $(" <div class=\"line_pro\"></div>")
    msg_pro.text(msg);
    msg_pro.appendTo(son_pro);
    line_pro.appendTo(son_pro);
    var num_pro = $(" <span class=\"radius_pro_son_text\"></span>");
    num_pro.text(num);
    var radius_pro_father = $("  <div class=\"radius_pro_father\"></div>");
    var radius_pro_son = $(" <i class=\"radius_pro_son\"></i>");
    num_pro.appendTo(radius_pro_son);
    radius_pro_son.appendTo(radius_pro_father);
    radius_pro_father.appendTo(son_pro);
    return son_pro;
};

var out_pro = function (num, msg) {
    var son_pro = $(" <div class=\"son_pro\"> </div>");
    var msg_pro = $("  <div class=\"msg_pro out_color_pro \"></div>");
    var line_pro = $(" <div class=\"line_pro out_color_pro\"></div>")
    msg_pro.text(msg);
    msg_pro.appendTo(son_pro);
    line_pro.appendTo(son_pro);
    var num_pro = $(" <span class=\"radius_pro_son_text\"></span>");
    num_pro.text(num);
    var radius_pro_father = $("  <div class=\"radius_pro_father out_color_pro\"></div>");
    var radius_pro_son = $(" <i class=\"radius_pro_son out_color_pro\"></i>");
    num_pro.appendTo(radius_pro_son);
    radius_pro_son.appendTo(radius_pro_father);
    radius_pro_father.appendTo(son_pro);
    return son_pro;
};